package com.chuangke.mds.controller;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.http.HttpResult;
import com.chuangke.elasticsearch.model.ElasticQuery;
import com.chuangke.mds.entity.Knowledge;
import com.chuangke.mds.service.KnowledgeEsService;
import com.chuangke.mds.service.KnowledgeSearchService;
import com.chuangke.mds.service.KnowledgeService;

@RestController
@RequestMapping("mds/knowledge")
public class KnowledgeController {

	@Autowired
	private KnowledgeService knowledgeService;
	@Autowired
	private KnowledgeSearchService knowledgeSearchService ;
	@Autowired
	private KnowledgeEsService knowledgeEsService ;

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/upload")
	public HttpResult upload(String knowledgeId, MultipartFile file) {
		return HttpResult.ok(knowledgeService.upload(knowledgeId, file));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/save")
	public HttpResult save(@RequestBody Knowledge record) throws ParseException {
		String id = knowledgeService.save(record) ;
		return HttpResult.ok(id);
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/delete")
	public HttpResult delete(@RequestBody List<String> billIds) {
		knowledgeService.delete(billIds);
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/deleteaffix")
	public HttpResult deleteAffix(@RequestBody List<String> ids) {
		knowledgeService.deleteAffix(ids);
		return HttpResult.ok();
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/page")
	public HttpResult page(@RequestBody Map<String, Object> params) {
		return HttpResult.ok(knowledgeService.page(new CkPageQuery(params)));
	}
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/pageVersion")
	public HttpResult pageVersion(@RequestBody Map<String, Object> params) {
		return HttpResult.ok(knowledgeService.pageVersion(new CkPageQuery(params)));
	}
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/loadVersion")
	public HttpResult loadVersion(@RequestParam String id) {
		return HttpResult.ok(knowledgeService.loadVersion(id));
	}
	

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/load")
	public HttpResult load(@RequestParam String id) {
		return HttpResult.ok(knowledgeService.load(id));
	}

	@PreAuthorize("hasAuthority('login:action')")
    @PostMapping("/search")
    public HttpResult search(@RequestBody ElasticQuery<Knowledge> elasticQuery) {
        return HttpResult.ok(knowledgeSearchService.search(elasticQuery));
    }
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/tt")
	public HttpResult tt() {
		knowledgeEsService.insert() ;
		return HttpResult.ok();
	}
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/getHomePagePopularData")
	public HttpResult getHomePagePopularData(@RequestBody Map<String, Object> params) {
		return HttpResult.ok(knowledgeService.getHomePagePopularData(new CkPageQuery(params)));
	}
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/getHomePageLatestData")
	public HttpResult getHomePageLatestData(@RequestBody Map<String, Object> params) {
		return HttpResult.ok(knowledgeService.getHomePageLatestData(new CkPageQuery(params)));
	}
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/asyncIncrementReadingCount")
	public HttpResult asyncIncrementReadingCount(@RequestBody Map<String, Object> params) {
		String id = (String) params.get("id");
		knowledgeService.asyncIncrementReadingCount(id);
		return HttpResult.ok("成功！");
	}
}
